<template>
  <div id="login">
    <p class="p1">
      <span>登录</span>
      <el-form ref="userForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" clearable />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password" show-password />
        </el-form-item>
        <el-form-item label="验证码" prop="vcode">
          <el-input type="password" v-model="form.vcode" class="in1" />
        </el-form-item>
        <p class="p2">
          验证码:
          {{ Math.floor(Math.random() * 9000) + 1000 }}
          <span class="sp1" @click="changeVcode">换一批</span>
        </p>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">提交</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </p>
  </div>
</template>

<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'Login',
  data () {
    return {
      form: {
        username: '',
        password: '',
        email: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        vcode: [
          { message: '请输入验证码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    randomFourDigitNumber () {
      return Math.floor(1000 + Math.random() * 9000) // 生成一个4位随机整数
    }
  },
  methods: {
    onSubmit () {
      this.$refs.userForm.validate((valid) => {
        if (valid) {
          this.$router.replace('/dashBoard')
        } else {
          alert('请输入用户名或密码')
          return false
        }
      })
    },
    reset () {
      this.$refs.userForm.resetFields()
    },
    changeVcode () {
      console.log(111)
      this.$router.replace('/login')
    }
  }
}
</script>
<style lang="scss" scoped>
#login {
  background: url("../../assets/pic2.webp");
  width: 100%;
  height: 100%;
  position: fixed;
  background-size: 100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .p1 {
    margin: auto;
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    width: 40%;
    height: 50%;
    background-color: #f5f5f5;
    opacity: 0.7;
    .in1 {
      width: 100px;
    }
    .p2 {
      flex: 1;
      color: red;
    }
    span {
      margin-left: 20px;
      font-weight: 800;
      color: black;
      display: block;
      margin-bottom: 30px;
    }
  }
}
</style>
